<?php

namespace App\Listeners\OpenShop;

use App\Enums\OpenShop\InvestmentPromotion\OpenShopCacheEnum;
use App\Enums\OpenShop\InvestmentPromotion\SignOrderEnum;
use App\Events\OpenShop\JoinSucceededSmsSentEvent;
use App\Events\OpenShop\OrderSignedEvent;
use App\Listeners\BaseListener;
use App\Models\OpenShop\InvestmentPromotion\OpenShopMainProcessModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopUserBaseInfoModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopUserIdentityCardModel;
use App\Service\Common\SmsService;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;

class SendSmsInformByJoinListener extends BaseListener
{
    const RMS_GET_SHOP_LIST_BY_USERID = 'api/internal/shop/shopListByPhone';

    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Handle the event.
     *
     * @param OrderSignedEvent $event
     * @return void
     * @throws GuzzleException
     */

    public function handle(OrderSignedEvent $event)
    {
        Log::info('加盟成功短信提醒事件:' . json_encode($event->order->toArray()));
        //发送签约完成短信提醒
        $order = $event->order;
        if (!$order instanceof OpenShopSignOrderModel) {
            return;
        }

        $userInfo = OpenShopUserIdentityCardModel::getDataByOrderNo($order->orderNo);
        $baseInfo = OpenShopUserBaseInfoModel::getDataByOrderNo($order->orderNo);

        //老店
        $param = [
            'name' => $userInfo->name,
        ];
        (new SmsService())->aliyunSend(optional($baseInfo)->mobile, config('sms.template2'), $param);


        Log::info('加盟成功短信提醒:' . '< 老店 >发送参数--' . json_encode($param));
        //根据订单号获得订单Id
        event(new JoinSucceededSmsSentEvent($order, $userInfo->name));
    }
}
